package com.wjy.icu.task;

import cn.hutool.core.thread.ThreadUtil;
import com.wjy.icu.domain.EquitmentBluetoothMonitor;
import com.wjy.icu.domain.vo.BluetoothVo;
import com.wjy.icu.emqx.utils.MqttUtils;
import com.wjy.icu.service.IBluetoochService;
import com.wjy.icu.service.IMqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @author: wangjunyong
 * @date: 2021/11/1 12:07
 */
@Component
@EnableScheduling
public class MonitoredCheck10Task {

    @Autowired
    IMqttService mqttService;
    @Autowired
    IBluetoochService bluetoochService;

    @Scheduled(cron = "0/2 * * * * ?")  //每3秒执行一次
    @Transactional
    public void configureTasks() throws InterruptedException {
        List<EquitmentBluetoothMonitor> equitmentBluetoothMonitors = bluetoochService.selectAllBluetooth();
        equitmentBluetoothMonitors.stream().forEach(equitmentBluetoothMonitor -> {
            Integer equitmentId = equitmentBluetoothMonitor.getEquitmentId();
            Integer monitorId = equitmentBluetoothMonitor.getMonitorId();
            BluetoothVo bluetoothVo = bluetoochService.findByEquitIdTask(equitmentId);
            if (bluetoothVo!=null) {
                String temp = bluetoothVo.getTemp();
                int state = bluetoothVo.getState();
                String mac = equitmentBluetoothMonitor.getName();
                if (state == 1) {
                    //发送宠物温度
                    String s6 = MqttUtils.lowHigh(temp);
                    mqttService.sendEquitment(mac, "0E", s6);
                }
            }
        });
    }
}
